package com.blast.comicsisland.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.blast.comicsisland.R;
import com.blast.comicsisland.adapters.TopicContentAdapter;
import com.blast.comicsisland.model.topic.InfoContent;
import com.blast.comicsisland.model.topic.SpecialDescriptionContent;
import com.blast.comicsisland.model.topic.TopicHeadList;
import com.blast.comicsisland.utils.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class TopicContentActivity extends AppCompatActivity {
    List<Map<String, Object>> list;
    private TextView textView;
    private ListView listView;
    private TopicContentAdapter adapter;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topic_content);

        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        String specialid = intent.getStringExtra("specialid");
        textView = (TextView) findViewById(R.id.topic_content_title);
        textView.setText(title);
        listView = (ListView) findViewById(R.id.topic_content);
        StringBuilder sb = new StringBuilder("http://mhjk.1391.com/comic/specialdesc");
        sb.append("?specialid=").append(specialid);
        sb.append("&e=1435064451&channelid=baidu");
        url = sb.toString();
        Log.d("------>","url"+url);
        list = new LinkedList<Map<String, Object>>();
        adapter = new TopicContentAdapter(this, list);
        listView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        TopicContentTask task = new TopicContentTask();
        task.execute(url);
    }

    public void btnBackOnClick(View view) {
        finish();
    }

    public class TopicContentTask extends AsyncTask<String, Void, JSONObject>{

        @Override
        protected JSONObject doInBackground(String... params) {
            JSONObject ret = null;
            String path = JsonUtils.doGetRequest(params[0]);
            try {
                if (path != null){
                    ret = new JSONObject(path);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.d("------>","url"+ret);
            return ret;
        }

        @Override
        protected void onPostExecute(JSONObject jsonObject) {
            super.onPostExecute(jsonObject);
            if (jsonObject != null){
                TopicHeadList topicContent = new TopicHeadList();
                topicContent.parseObject(jsonObject);
                InfoContent infoContent = topicContent.getInfo();
                List<SpecialDescriptionContent> sdcs = infoContent.getSpecialdescriptioncontent();
                int length = sdcs.size();
                for (int i = 0; i < length; i++) {
                    SpecialDescriptionContent sdc = new SpecialDescriptionContent();
                    sdc = sdcs.get(i);
                    int targetmethod = sdc.getTargetmethod();
                    String targetargument = sdc.getTargetargument();
                    String coverurl = sdc.getCoverurl();
                    Log.d("------>","coverurl"+url);
                    String description = sdc.getDescription();
                    Log.d("------>","description"+description);
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("type", targetmethod);
                    map.put("targetargument", targetargument);
                    map.put("coverurl", coverurl);
                    map.put("description", description);
                    list.add(map);
                }
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_topic_content, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
